<?php

namespace zhtfrontend\controllers;

use common\components\extenders\AjaxObject;

use common\components\helpers\Fun;
use common\models\datas\Customer;
use common\models\datas\CustomerDues;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\data\Pagination;
use yii\helpers\Json;
use yii\widgets\ActiveForm;


class DuesController extends \zhtfrontend\components\bases\BaseController
{

    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = CustomerDues::find()->where(['site_id' => Fun::getSiteId()])->orderBy('id DESC');
        if (!empty($param['searchData'])) {
            $search_data = $param['searchData'];
            if (!empty($search_data['tel'])) {
                $customer_ids = Customer::find()->select('id')->where(['like', 'mobile', $search_data['tel']])->andWhere(['site_id' => Fun::getSiteId()])->column();
                $data->andWhere(['customer_id' => $customer_ids]);
            }
            if (!empty($search_data['name'])) {
                $customer_ids = Customer::find()->select('id')->where(['like', 'name', $search_data['name']])->andWhere(['site_id' => Fun::getSiteId()])->column();
                $data->andWhere(['customer_id' => $customer_ids]);
            }
        }
        $totals = $data->sum('money');
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['customer_name', 'pay_name']);
        $content = "<h4 class='yx-total1 yx-floatl'>总金额：<span class='yx-color2cb4f3 yx-font16 yx-mr5'>{$totals}</span>元</h4><div class='yx-clear'></div>";

        return $this->render('index', ['data' => $array, 'pages' => $pages, 'content' => $content]);
    }


}